home *** CD-ROM | disk | FTP | other *** search
- /* ***** BEGIN LICENSE BLOCK *****
- * Version: MPL 1.1/GPL 2.0/LGPL 2.1
- *
- * The contents of this file are subject to the Mozilla Public License Version
- * 1.1 (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- * http://www.mozilla.org/MPL/
- *
- * Software distributed under the License is distributed on an "AS IS" basis,
- * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
- * for the specific language governing rights and limitations under the
- * License.
- *
- * The Original Code is Forecastfox.
- *
- * The Initial Developer of the Original Code is
- * Jon Stritar <jstritar@MIT.EDU>.
- * Portions created by the Initial Developer are Copyright (C) 2005
- * the Initial Developer. All Rights Reserved.
- *
- * Contributor(s):
- * Jon Stritar <jstritar@MIT.EDU>
- * Richard Klein <richwklein@mchsi.com>
- *
- * Alternatively, the contents of this file may be used under the terms of
- * either the GNU General Public License Version 2 or later (the "GPL"), or
- * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
- * in which case the provisions of the GPL or the LGPL are applicable instead
- * of those above. If you wish to allow use of your version of this file only
- * under the terms of either the GPL or the LGPL, and not to allow others to
- * use your version of this file under the terms of the MPL, indicate your
- * decision by deleting the provisions above and replace them with the notice
- * and other provisions required by the GPL or the LGPL. If you do not delete
- * the provisions above, a recipient may use your version of this file under
- * the terms of any one of the MPL, the GPL or the LGPL.
- *
- * ***** END LICENSE BLOCK ***** */
-
- const CLASS_ID = Components.ID("{71B45B67-5AC7-4aaf-8043-A3DD45C67636}");
- const CLASS_NAME = "Forecastfox Ping Component";
- const CONTRACT_ID = "@ensolis.com/forecastfox/ping;1";
- const ffIPing = Components.interfaces.ffIPing;
-
- /******************************************************************************
- * ffPing Component
- ******************************************************************************/
- function ffPing() {};
- ffPing.prototype = {
- _URL: "http://forecastfox.ensolis.com/logs/log.php",
- _manager: null,
- _ds: null,
- _obs: null,
- _downgrad: false,
- get url() { return this._URL; },
-
- start: function()
- {
- this._manager = Components.classes["@ensolis.com/forecastfox/manager;1"].getService(Components.interfaces.ffIManager);
-
- //figure out if we are toolkit or mozilla
- var isToolkit = true;
- var isDeerPark = true;
- try {
- var em = Components.classes["@mozilla.org/extensions/manager;1"].getService(Components.interfaces.nsIExtensionManager);
- } catch(e) {
- isToolkit = false;
- };
-
- //toolkit so add observers
- if (isToolkit) {
- try {
- var test = em.getItemForID("{0538E3E3-7E9B-4d49-8831-A227C80A7AD3}");
- } catch(e) {
- isDeerPark = false;
- };
-
- if (isDeerPark) {
- this._obs = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
- this._obs.addObserver(this, "em-action-requested", false);
- } else {
- this._ds = em.datasource;
- this._ds.AddObserver(this);
- };
- };
- },
-
- stop: function()
- {
- //unregister as an observer of the em datasource
- if (this._ds)
- this._ds.RemoveObserver(this);
-
- if (this._obs)
- this._obs.removeObserver(this, "em-action-requested");
-
- //remove variables
- this._manager = null;
- this._ds = null;
- this._obs = null;
- this._downgrade = null;
- },
-
- open: function()
- {
- //get pref branch
- var last = this._manager.branch.getCharPref("pinged");
- var migrated = this._manager.branch.getCharPref("migrated");
-
- //use migrated value if last is blank
- if (last == "" && migrated != "")
- last = migrated;
-
- switch (last) {
- case "0.8.5":
- break;
- case "":
- case "*":
- this._open(ffIPing.OPEN_INSTALL, null, "0.8.5");
- break;
- default:
- this._open(ffIPing.OPEN_UPGRADE, last, "0.8.5");
- break;
- };
-
- //set pref
- this._manager.branch.setCharPref("pinged", "0.8.5");
- },
-
- _open: function(aType, aFromVer, aToVer)
- {
- //create query url
- var query = this._URL + "?type=" + aType;
- switch (aType) {
- case ffIPing.OPEN_UNINSTALL:
- query = query + "&from=" + aFromVer;
- break;
- case ffIPing.OPEN_UPGRADE:
- query = query + "&from=" + aFromVer + "&to=" + aToVer;
- break;
- case ffIPing.OPEN_INSTALL:
- query = query + "&to=" + aToVer;
- break;
- };
-
- query = query + "&guid=" + this._manager.guid;
-
- //send ping request
- var request = Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Components.interfaces.nsIXMLHttpRequest);
- request.open("GET", query, true);
- try {
- if (request.channel instanceof Components.interfaces.nsISupportsPriority)
- request.channel.priority = Components.interfaces.nsISupportsPriority.PRIORITY_HIGHEST;
- } catch(e) {};
- request.send(null);
- },
-
- _dsObserve: function(aDS, aSource, aProperty, aTarget)
- {
- if (aSource.ValueUTF8 != "urn:mozilla:extension:" + "{0538E3E3-7E9B-4d49-8831-A227C80A7AD3}")
- return;
-
- var prefix = "http://www.mozilla.org/2004/em-rdf#";
- var action = aProperty.ValueUTF8.replace(prefix, "");
- var pinged = null;
- switch (action) {
- case "toBeUninstalled":
- this._open(ffIPing.OPEN_UNINSTALL, "0.8.5", null);
- pinged = "*";
- break;
- case "toBeInstalled":
- this._downgrade = true;
- case "version":
- if (this._downgrade) {
- var em = Components.classes["@mozilla.org/extensions/manager;1"].getService(Components.interfaces.nsIExtensionManager);
- var item = em.getItemList("{0538E3E3-7E9B-4d49-8831-A227C80A7AD3}", Components.interfaces.nsIUpdateItem.TYPE_EXTENSION, {})[0];
- try {
- var vc = Components.classes["@mozilla.org/updates/version-checker;1"].getService(Components.interfaces.nsIVersionChecker);
- } catch(e) {
- vc = Components.classes["@mozilla.org/xpcom/version-comparator;1"].getService(Components.interfaces.nsIVersionComparator);
- };
- if (vc.isValidVersion(item.version)) {
- if ((vc.compare(item.version, "0.7.1") <= 0) && (vc.compare(item.version, "0.5.9") > 0)) {
- pinged = item.version;
- this._open(ffIPing.OPEN_UPGRADE, "0.8.5", pinged);
- } else if (vc.compare(item.version, "0.5.9") <= 0) {
- // if it is a downgrade to a version we do not track, then report it as an uninstall
- this._open(ffIPing.OPEN_UNINSTALL, "0.8.5", null);
- pinged = "*";
- };
- };
- };
- break;
- };
-
- if (pinged)
- this._manager.branch.setCharPref("pinged", pinged);
- },
-
- ////////////////////////////
- // nsIObserver
- observe: function(aSubject, aTopic, aData)
- {
- var item = aSubject.QueryInterface(Components.interfaces.nsIUpdateItem);
- if (item.id != "{0538E3E3-7E9B-4d49-8831-A227C80A7AD3}")
- return;
-
- var pinged = null;
- switch (aData) {
- case "item-uninstalled":
- this._open(ffIPing.OPEN_UNINSTALL, "0.8.5", null);
- pinged = "*";
- break;
- case "item-upgraded":
- try {
- var vc = Components.classes["@mozilla.org/updates/version-checker;1"].getService(Components.interfaces.nsIVersionChecker);
- } catch(e) {
- vc = Components.classes["@mozilla.org/xpcom/version-comparator;1"].getService(Components.interfaces.nsIVersionComparator);
- };
- if (vc.isValidVersion(item.version) && (vc.compare(item.version, "0.7.1") <= 0)) {
- pinged = item.version;
- this._open(ffIPing.OPEN_UPGRADE, "0.8.5", pinged);
- };
- break;
- };
-
- if (pinged)
- this._manager.branch.setCharPref("pinged", pinged);
- },
-
- ///////////////////////////
- // nsIRDFObserver
- onAssert: function(aDS, aSource, aProperty, aTarget) { this._dsObserve(aDS, aSource, aProperty, aTarget); },
- onUnassert: function(aDS, aSource, aProperty, aTarget) {},
- onChange: function(aDS, aSource, aProperty, aOldTarget, aNewTarget) { this._dsObserve(aDS, aSource, aProperty, aNewTarget); },
- onMove: function(aDS, aOldSource, aNewSource, aProperty, aTarget) {},
- onBeginUpdateBatch: function(aDS) {},
- onEndUpdateBatch: function(aDS) {},
-
- ///////////////////////////
- // nsIClassInfo
- getInterfaces: function(aCount)
- {
- var ifaces = new Array();
- ifaces.push(Components.interfaces.ffIPing);
- ifaces.push(Components.interfaces.nsIRDFObserver);
- ifaces.push(Components.interfaces.nsIObserver);
- ifaces.push(Components.interfaces.nsIClassInfo);
- ifaces.push(Components.interfaces.nsISupports);
- aCount.value = ifaces.length;
- return ifaces;
- },
-
- getHelperForLanguage: function(aLanguage) { return null; },
- get contractID() { return CONTRACT_ID; },
- get classID() { return CLASS_ID; },
- get classDescription() { return CLASS_NAME; },
- get implementationLanguage() { return Components.interfaces.nsIProgrammingLanguage.JAVASCRIPT; },
- get flags() { return Components.interfaces.nsIClassInfo.SINGLETON; },
-
- ///////////////////////////
- // nsISupports
- QueryInterface: function (aIID)
- {
- if (!aIID.equals(Components.interfaces.ffIPing) &&
- !aIID.equals(Components.interfaces.nsIRDFObserver) &&
- !aIID.equals(Components.interfaces.nsIObserver) &&
- !aIID.equals(Components.interfaces.nsIClassInfo) &&
- !aIID.equals(Components.interfaces.nsISupports))
- throw Components.results.NS_ERROR_NO_INTERFACE;
- return this;
- }
- };
-
- /******************************************************************************
- * XPCOM Functions for construction and registration
- ******************************************************************************/
- var gModule = {
- _firstTime: true,
- registerSelf: function(aCompMgr, aFileSpec, aLocation, aType)
- {
- if (this._firstTime) {
- this._firstTime = false;
- throw Components.results.NS_ERROR_FACTORY_REGISTER_AGAIN;
- };
- aCompMgr = aCompMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
- aCompMgr.registerFactoryLocation(CLASS_ID, CLASS_NAME, CONTRACT_ID, aFileSpec, aLocation, aType);
- },
-
- unregisterSelf: function(aCompMgr, aLocation, aType)
- {
- aCompMgr = aCompMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
- aCompMgr.unregisterFactoryLocation(CLASS_ID, aLocation);
- },
-
- getClassObject: function(aCompMgr, aCID, aIID)
- {
- if (!aIID.equals(Components.interfaces.nsIFactory))
- throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
-
- if (aCID.equals(CLASS_ID))
- return gFactory;
-
- throw Components.results.NS_ERROR_NO_INTERFACE;
- },
-
- canUnload: function(aCompMgr) { return true; }
- };
-
- var gFactory = {
- createInstance: function (aOuter, aIID)
- {
- if (aOuter != null)
- throw Components.results.NS_ERROR_NO_AGGREGATION;
- return (new ffPing()).QueryInterface(aIID);
- }
- };
-
- function NSGetModule(aCompMgr, aFileSpec) { return gModule; }